/**
 * 路由管理对象
 */
import Vue from "vue";
import VueRouter from "vue-router";
import home from "../views/home.vue";
import foodDetail from "../views/foodDetail.vue";
import chooseFood from "../views/chooseFood.vue";
import order from "../views/order.vue";
import category from "../views/category.vue";
import my from "../views/my.vue";
import login from "@/views/login";
import receiveAddress from "@/views/receiveAddress";
import addAddress from "@/views/addAddress";
import store from "@/store";

//最重要的一步，在vue有webpack开发的时候，所有全家桶的插件都要执行use
Vue.use(VueRouter);

const router = new VueRouter({
    mode: "hash",
    routes: [
        {
            path: "/",
            redirect: "/home"
        },
        {
            path: "/home",
            name: "home",
            component: home,
            children: [
                {
                    path: "/",
                    redirect: {
                        name: "chooseFood"
                    }
                },
                {
                    path: "chooseFood",
                    name: "chooseFood",
                    component: chooseFood,
                    meta:{
                        title:"点餐",
                        index:1
                    }
                }, {
                    path: "order",
                    name: "order",
                    component: order,
                    meta:{
                        title:"订单",
                        index:2
                    }
                }, {
                    path: "category",
                    name: "category",
                    component: category,
                    meta:{
                        title:"分类",
                        index:3
                    }
                }, {
                    path: "my",
                    name: "my",
                    component: my,
                    meta:{
                        title:"我的",
                        index:4
                    }
                }
            ]
        }, {
            path: "/foodDetail/:id",
            name: "foodDetail",
            component: foodDetail,
            meta:{
                title: "菜品详细",
                index:11
            }
        }, {
            path: "/login",
            name: "login",
            component: () => import("@/views/login"),
            meta:{
                title: "登录",
                index:20
            }

        }, {
            path: "/shopCartList",
            name: "shopCartList",
            component: () => import("@/views/shopCartList"),
            meta: {
                requireAuth: true,
                title: "购物车",
                index: 12
            }
        }, {
            path: "/receiveAddress",
            name: "receiveAddress",
            component: () => import("@/views/receiveAddress"),
            meta: {
                requireAuth: true,
                title: "收货地址",
                index: 13
            }
        },
        {
            path: "/addAddress",
            name: "addAddress",
            component: () => import("@/views/addAddress"),
            meta:{
                requireAuth: true,
                title:"添加收货地址",
                index: 14
            }
        }
    ]
});


// 路由的导航守卫
// 在进入某一个路由之前需要判断是否可以进入
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    // 如果这个路由上面的meta里面有requireAuth这个属性，并且为true，那么进入这个路由就需要token
    if (to.meta.requireAuth === true){
        if (store.state.softeem_ele_client_token){
            next();
        }else {
            next({
                name:"login"
            });
        }
    }else {
        next();
    }
})
export default router;